<template>
  <div class="row">
    <div class="col">
      <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-collapse-transition>
          <div class="login_pop" v-show="show4">
            <div id="mb_msg" v-text="error"></div>
          </div>
        </el-collapse-transition>
        <el-container v-show="show1">
          <el-header class="hea">
            <p class="Setting" v-text="text.setting"></p>
            <p class="num" v-text="text.num"></p>
          </el-header>
          <el-main class="tk-main">
            <div class="container-fluid">
              <div class="row justify-content-md-center">
                <div class="col">
                  <div class="form-group row">
                    <el-select class="small" v-model="ruleForm.area">
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      ></el-option>
                    </el-select>
                    <el-input
                      class="col-8 tright"
                      v-model="ruleForm.phonenum"
                      name="phonenum"
                      placeholder="请输入手机号码"
                    ></el-input>
                  </div>
                  <div class="form-group row">
                    <el-input class="small" v-model="ruleForm.yzm" placeholder="填写验证码"></el-input>
                    <button
                      v-if="isTel"
                      class="col-8 btn btn-block primary"
                      @click="getyzm()"
                      type="button"
                    >获取验证码</button>
                    <button
                      v-else
                      class="col-7 btn btn-block primary turnright"
                      disabled
                      type="button"
                      v-text="times()"
                    ></button>
                  </div>
                  <div class="form-group text-center">
                    <button type="button" @click="next()" class="btn primary btn-block">下一步</button>
                  </div>
                </div>
              </div>
            </div>
          </el-main>
        </el-container>
        <div class="appbig" v-show="show2">
          <p class="Setting">设置用户名与密码</p>
          <p class="num">密码由8-16位数字、字母或符号组成。</p>
          <div class="form-group">
            <el-input
              v-model="ruleForm.username"
              name="username"
              placeholder="请输入用户名"
              style="font-size:15px"
            >
              <i v-if="show7" class="el-icon-check el-input__icon text-success" slot="suffix"></i>
              <i v-else class="el-icon-edit el-input__icon" slot="suffix"></i>
            </el-input>
            <el-collapse-transition>
              <div class="tishi" v-show="show7">
                <div>
                  <i class="icon iconfont" :class="ichange('err1')"></i> &nbsp;用户名不能为空
                </div>
              </div>
            </el-collapse-transition>
          </div>
          <div class="form-group">
            <el-input
              type="password"
              id="pass1"
              v-model="ruleForm.password"
              name="password"
              placeholder="请输入密码"
              style="font-size:15px"
            >
              <i
                v-on:click=" mousedown1(1)"
                v-on:mouseup="mouseup1(1)"
                class="icon iconfont icon-bukejian el-input__icon chead"
                slot="suffix"
              ></i>
            </el-input>
            <el-collapse-transition>
              <div class="tishi" v-show="show8">
                <div>
                  <i class="icon iconfont" :class="ichange('err2')"></i> &nbsp;不能包含空格
                </div>
                <div>
                  <i class="icon iconfont" :class="ichange('err3')"></i>
                  &nbsp;长度为8-16个字符
                </div>
                <div>
                  <i class="icon iconfont" :class="ichange('err4')"></i>
                  &nbsp;必须包含字母和数字
                </div>
              </div>
            </el-collapse-transition>
          </div>
          <div class="form-group">
            <el-input
              type="password"
              id="pass2"
              v-model="ruleForm.password2"
              placeholder="请再次输入密码"
              style="font-size:15px"
            >
              <i
                v-on:click="mousedown1(2)"
                v-on:mouseup="mouseup1(2)"
                class="icon iconfont icon-bukejian el-input__icon chead"
                slot="suffix"
              ></i>
            </el-input>
            <el-collapse-transition>
              <div class="tishi" v-show="show9">
                <div>
                  <i class="icon iconfont" :class="ichange('err5')"></i> &nbsp;两次输入不同
                </div>
              </div>
            </el-collapse-transition>
          </div>
          <div class="block">
            <span class="demonstration">省市</span>
            <el-cascader
              style="width:229px"
              :options="address()"
              :props="{value:'label'}"
              v-model="selectedOptions"
              @change="handleChange"
            ></el-cascader>
          </div>
          <div class="zhuce">
            <p class="zhangh" @click="submitForm()">注册账户</p>
          </div>
        </div>
        <div class="app" v-show="show3">
          <div class="tupian">
            <img src="https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/right.png" alt />
          </div>
          <p class="fsong">您提交的信息已发送后台审核，审核通过后您会接收到短信通知，并可登陆哈工科教云课堂www.hterobot.com</p>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import $ from "jquery";
import schema from "async-validator";
import { asyncregist_getCode, asyncregist } from "../plugins/api.js";
export default {
  data() {
    return {
      selectedOptions: [],
      text: {
        setting: "输入短信验证码",
        num: "请输入手机号帮助我们确认您的身份。"
      },
      ruleForm: {
        username: "",
        password: "",
        password2: "",
        phonenum: "",
        packegeid: "",
        agent: "",
        area: "0086",
        yzm: "",
        address: {
          province: "",
          city: ""
        }
      },
      iserr: {
        err1: false,
        err2: false,
        err3: false,
        err4: false,
        err5: false
      },
      show1: true,
      show2: false,
      show8: false,
      show7: false,
      show3: false,
      show9: false,
      show4: false,
      options: [
        {
          value: "0086",
          label: "+86"
        },
        {
          value: "+886",
          label: "+886"
        }
      ],
      time: 60,
      timer: Object,
      isTel: false,
      error: ""
    };
  },
  async mounted() {
    var data = GetRequest();
    this.ruleForm.packegeid = data.packegeid ? data.packegeid : data.packege_ID;
    this.ruleForm.agent = data.agent;
    if (this.ruleForm.address) {
      this.selectedOptions = [
        this.ruleForm.address.province,
        this.ruleForm.address.city,
        this.ruleForm.address.area
      ];
    }
  },
  watch: {
    "ruleForm.phonenum"(v) {
      var reg = new RegExp(/^1[3456789]\d{9}$/);
      if (reg.test(v)) {
        this.isTel = true;
      }
    },
    time(v) {
      if (v == 60) {
        this.isTel = true;
      } else {
        this.isTel = false;
      }
    },
    "ruleForm.username": function(v) {
      console.log(v);
      this.jiance(1);
    },
    "ruleForm.password": function() {
      this.jiance(2);
      this.jiance(3);
    },
    "ruleForm.password2": function() {
      this.jiance(3);
    },
    "iserr.err1": function(v) {
      console.log(v);
      this.show7 = !v;
    },
    "iserr.err2": function(v) {
      this.show8 = !(v && this.iserr.err3 && this.iserr.err4);
    },
    "iserr.err3": function(v) {
      this.show8 = !(v && this.iserr.err2 && this.iserr.err4);
    },
    "iserr.err4": function(v) {
      this.show8 = !(v && this.iserr.err3 && this.iserr.err2);
    },
    "iserr.err5": function(v) {
      console.log(v);
      this.show9 = !this.iserr.err5;
    }
  },
  methods: {
    mouseup1: function(i) {
      if (i === 1) {
        $("#pass1").prop("type", "password");
      }
      if (i === 2) {
        $("#pass2").prop("type", "password");
      }
    },
    mousedown1: function(i) {
      if (i === 1) {
        $("#pass1").prop("type", "text");
      }
      if (i === 2) {
        $("#pass2").prop("type", "text");
      }
    },
    ichange: function(i) {
      if (this.iserr[i]) {
        return "icon-zhengque text-success";
      } else {
        return "icon-cuowu text-danger";
      }
    },
    address() {
      const address = window.address || [];
      return address;
    },
    handleChange() {
      const data = {
        province: this.selectedOptions[0],
        city: this.selectedOptions[1],
        area: this.selectedOptions[2]
      };
      this.ruleForm.address = data;
    },
    async getyzm() {
      var reg = new RegExp(/^1[3456789]\d{9}$/);
      if (!reg.test(this.ruleForm.phonenum)) {
        this.tshow4("手机格式不正确");
        return false;
      }
      try {
        await asyncregist_getCode({
          phonenumber: this.ruleForm.phonenum
        });
        this.timer = setInterval(this.hc, 1000);
      } catch (err) {
        this.tshow4(err);
      }
    },
    hc() {
      var a = this.time;
      this.time--;
      if (a == 0) {
        clearInterval(this.timer);
        this.time = 60;
      }
    },
    tshow4(text) {
      this.error = text;
      this.show4 = true;
    },
    next() {
      var reg = new RegExp(/^1[3456789]\d{9}$/);
      if (!reg.test(this.ruleForm.phonenum)) {
        this.tshow4("手机格式不正确");
        return false;
      }
      if (!this.ruleForm.yzm) {
        this.tshow4("请填写验证码");
        return;
      }
      this.show1 = false;
      this.show2 = true;
      this.show3 = false;
      this.show4 = false;
    },
    async submitForm() {
      if (this.iserr.err1 == false) {
        this.show7 = true;
        return false;
      }
      if (this.iserr.err2 == false) {
        this.show8 = true;
        return false;
      }
      if (this.iserr.err3 == false) {
        this.show8 = true;
        return false;
      }
      if (this.iserr.err4 == false) {
        this.show8 = true;
        return false;
      }
      if (this.iserr.err5 == false) {
        this.show9 = true;
        return false;
      }
      console.log(this.ruleForm.address.city);
      if (this.ruleForm.address.city == "") {
        this.$message({
          message: " 城市信息必填，请认真填写!"
        });
        return false;
      }
      const descriptor = {
        password: {
          type: "string",
          required: true,
          min: 6,
          max: 32,
          message: "密码长度6-32"
        },
        phonenum: {
          type: "regexp",
          regexp: new RegExp(/^1[3456789]\d{9}$/),
          required: true,
          message: "手机格式填写不正确"
        }
      };
      var validator = new schema(descriptor);
      validator.validate(this.ruleForm, (errors, fields) => {
        if (errors) {
          for (let v in fields) {
            this.tshow4(fields[v][0].message);
          }
        } else {
          this.show4 = false;
        }
      });
      try {
        await asyncregist(this.ruleForm);
        this.show2 = false;
        this.show3 = true;
      } catch (err) {
        this.tshow4(err);
      }
    },
    times: function() {
      if (this.time == 60) {
        return "请输入验证码";
      } else {
        return `${this.time} s`;
      }
    },
    jiance: function(i) {
      if (i === 1) {
        console.log(456);
        this.iserr.err1 = jiankong(this.ruleForm.username);
      }

      if (i === 2) {
        this.iserr.err2 = jiankongge(this.ruleForm.password);

        if (
          this.ruleForm.password.length >= 8 &&
          this.ruleForm.password.length <= 16
        ) {
          this.iserr.err3 = true;
        } else {
          this.iserr.err3 = false;
        }
        this.iserr.err4 = jianliangzhong(this.ruleForm.password);
      }

      if (i === 3) {
        if (this.ruleForm.password == this.ruleForm.password2) {
          console.log(1);
          this.iserr.err5 = true;
        } else {
          console.log(2);
          this.show9 = true;
          this.iserr.err5 = false;
        }
      }
      if (i === 4) {
        this.iserr.err6 = jianphone(this.ruleForm.phonenum);
      }

      function jiankong(data) {
        if (data === "") {
          return false;
        } else {
          return true;
        }
      }

      function jiankongge(data) {
        var reg = new RegExp(/^\s*(\S+)\s*$/);
        if (reg.test(data)) {
          return true;
        } else {
          return false;
        }
      }

      function jianliangzhong(data) {
        var reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
        if (reg.test(data)) {
          return true;
        } else {
          return false;
        }
      }

      function jianphone(data) {
        var reg = new RegExp(/^1[3456789]\d{9}$/);
        if (reg.test(data)) {
          return true;
        } else {
          return false;
        }
      }
    },
    des: function(time) {
      return new Promise(function(res) {
        setTimeout(res(true), time);
      });
    }
  }
};

function GetRequest() {
  var url = location.search;
  var theRequest = new Object();
  if (url.indexOf("?") != -1) {
    var str = url.substr(1);
    const strs = str.split("&");
    for (var i = 0; i < strs.length; i++) {
      theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }
  }
  return theRequest;
}
</script>
<style>
* {
  margin: 0;
  padding: 0;
}

.appbig {
  margin: 25px 30px 0;
}

.Setting {
  /* text-align: center; */
  font-family: MicrosoftYaHei;
  font-size: 1.5rem;
  font-weight: 400;
  font-stretch: normal;
  margin-bottom: 2px;
}

.num {
  font-size: 12px;
  font-weight: bold;
  font-stretch: normal;
  color: #999999;
}

.inpol {
  width: 100%;
  height: 58px;
  outline: none;
  color: #999999;
  margin-bottom: 5px;
  border-bottom: 2px solid #f6f6f6 !important;
}

.inpox {
  width: 100%;
  height: 58px;
  outline: none;
  color: #999999;
  margin-bottom: 5px;
  border-bottom: 2px solid #f6f6f6 !important;
}

.zhuce {
  width: 100%;
  height: 40px;
  color: #ffffff;
  background: #2f81f1;
  border-radius: 5px;
  cursor: pointer;
}

.zhangh {
  margin-top: 5px;
  font-size: 17px;
  text-align: center;
  line-height: 40px;
}

/***************************************************************/
.appbig {
  margin: 25px 30px 0;
}

.Setting {
  /* text-align: center; */
  font-family: MicrosoftYaHei;
  font-size: 1.5rem;
  font-weight: 400;
  font-stretch: normal;
  margin-bottom: 2px;
}

.num {
  font-size: 12px;
  font-weight: bold;
  font-stretch: normal;
  color: #999999;
}

.volo {
  width: 25%;
  height: 58px;
  outline: none;
  float: left;
  margin-bottom: 5px;
  border-bottom: 2px solid #f6f6f6 !important;
  color: #999999;
}

.inpo {
  width: 70%;
  height: 58px;
  float: right;
  outline: none;
  color: #999999;
  margin-bottom: 5px;
  border-bottom: 2px solid #f6f6f6 !important;
}

.inpoy {
  width: 33%;
  height: 58px;
  outline: none;
  color: #999999;
  margin-bottom: 5px;
  border-bottom: 2px solid #f6f6f6 !important;
}

/* .zhuce {
		width: 100%;
		height: 40px;
		color: #999999;
		background: #E9EBEC;
		border-radius: 5px;
	} */

.zhangh {
  margin-top: 5px;
  font-size: 17px;
  text-align: center;
  line-height: 40px;
}

.fs {
  width: 50%;
  height: 40px;
  margin-top: 15px;
  color: #ffffff;
  background: #2f81f1;
  border-radius: 5px;
  float: right;
  cursor: pointer;
}

.yanzheng {
  font-size: 17px;
  text-align: center;
  line-height: 40px;
}

/**********************************************/
.app {
  margin: 25px 30px 0;
}

.tupian {
  width: 100%;
  height: 130px;
  margin: 0 auto;
  text-align: -webkit-center;
}

.fsong {
  font-family: MicrosoftYaHeiLight;
  font-size: 16px;
  font-weight: normal;
  font-stretch: normal;
  letter-spacing: 3px;
  color: #464444;
  text-align: center;
}

/***********************************************/
html,
body,
#app,
.el-container {
  height: 100%;
  font-family: "MicrosoftYaHei";
}

body {
  background-color: #ffffff;
}

img {
  margin-top: 10px;
}

.tk-main {
  -moz-box-shadow: 5px 5px 5px #eee inset;
  /* For Firefox3.6+ */
  -webkit-box-shadow: 5px 5px 5px #eee inset;
  /* For Chrome5+, Safari5+ */
  box-shadow: 5px 5px 5px #eee inset;
  /* For Latest Opera */
  /* padding-top: 204px; */
}

#wrapper {
  display: table;
}

#cell {
  display: table-cell;
  vertical-align: middle;
}

#floater {
  float: left;
  height: 50%;
  margin-bottom: -120px;
}

#content {
  clear: both;
  height: 240px;
  position: relative;
}

.el-icon-success {
  font-size: 56px;
  color: green;
}

.label {
  color: #565656;
  font-size: 16px;
}

#app input {
  border-radius: 0px;
  font-size: 12px;
  color: rgba(70, 68, 68, 0.6);
}

.tishi {
  padding: 10px;
}

.chead {
  cursor: pointer;
}

.icon-bukejian:hover {
  color: #000000;
}

.primary {
  background-color: #2f81f1;
  color: azure;
}

.turnright {
  margin-left: 14px;
}

.tright {
  margin-left: 2px;
}

.small {
  width: 28%;
  margin-left: 15px;
}

.hea {
  padding: 0 37px;
}

.Setting {
  /* text-align: center; */
  font-family: MicrosoftYaHei;
  font-size: 1.5rem;
  font-weight: 400;
  font-stretch: normal;
  margin-bottom: 2px;
}

.num {
  font-size: 12px;
  font-weight: bold;
  font-stretch: normal;
  color: #999999;
}

.el-input__inner {
  border-style: none;
  background: #ffffff;
  border-bottom: 2px solid #ededed;
}

.tk-main {
  box-shadow: 5px 5px 5px #ffffff;
}

.form-group {
  flex-wrap: inherit !important;
}

.el-input__inner {
  border-style: none;
  background: #ffffff;
  border-bottom: 2px solid #ededed !important;
}

.justify-content-md-center {
  justify-content: end !important;
}

.wrap {
  max-width: 640px;
  height: 100vh;
  background: #eee;
  margin: 0px auto;
  text-align: center;
}

.login_btn {
  width: 80px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background: orange;
  color: #fff;
  border-radius: 5px;
  cursor: pointer;
  margin-top: 30vh;
}

.login_pop {
  position: fixed;
  left: 0px;
  right: 0px;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  padding: 10px 0px;
  top: 0;
}

.login_pop #mb_ico {
  position: absolute;
  right: 10px;
  top: 10px;
  font-size: 16px;
  display: block;
  padding: 0px 10px;
}

.login_pop #mb_msg {
  text-align: center;
}
.demonstration {
  font-size: 15px;
  color: #999999;
}
.el-input__inner {
  padding: 0;
}
</style>
